/**
 * 连接后台服务
 */
angular.module('ddapp.service', [
	"ngResource",
	"ngStorage"
])

//菜单服务
.service('MenuService', ['$resource', function($resource){
	var menures = $resource(baseurl+'/menu/:rid');//菜单
	var menu = null;
	
	//获取菜单
	this.getmenu = function(rid){
		if (menu != null)
			return menu;
		menu = menures.query(
			{rid:rid}, 
			function(menu){
				for (var i=0; i<menu.length; ++i) {
					var menuitem = menu[i];
					if (!menuitem.cimage) {
						menuitem.cimage = "images/cai.jpg";
					}
					//
					for (var j=0; j<menuitem.disheslist.length; ++j) {
						var dishes = menuitem.disheslist[j];
						dishes.buycount = 0;
						if (!dishes.dimage) {
							dishes.dimage = "images/cai.jpg";
						}
					}
				}
			}
		);
		return menu;
	};
	
	//获取菜品详情
	this.getdetail = function(cid, did){
		//
		var menuitem = null;
		for (var i=0; i<menu.length; ++i) {
			menuitem = menu[i];
			if (menuitem.cid == cid)
				break;
		}
		//
		var dishes = null;
		for (var j=0; j<menuitem.disheslist.length; ++j) {
			dishes = menuitem.disheslist[j];
			if (dishes.did == did) {
				break;
			}
		}
		//
		return {cid:cid, cname:menuitem.cname, did:did, dname:dishes.dname, dprice:dishes.dprice, dimage:dishes.dimage};
	};
	//增加购买
	this.addgoods = function(cid, did){
		//
		var menuitem = null;
		for (var i=0; i<menu.length; ++i) {
			menuitem = menu[i];
			if (menuitem.cid == cid)
				break;
		}
		//
		for (var j=0; j<menuitem.disheslist.length; ++j) {
			dishes = menuitem.disheslist[j];
			if (dishes.did == did) {
				++dishes.buycount;
				break;
			}
		}
	};
	//减少购买
	this.removegoods = function(cid, did){
		//
		var menuitem = null;
		for (var i=0; i<menu.length; ++i) {
			menuitem = menu[i];
			if (menuitem.cid == cid)
				break;
		}
		//
		for (var j=0; j<menuitem.disheslist.length; ++j) {
			dishes = menuitem.disheslist[j];
			if (dishes.did == did) {
				if (dishes.buycount > 0) {
					--dishes.buycount;
				}
				break;
			}
		}
	};
	//清除购物车
	this.cleangoods = function(cid, did){
		//
		var menuitem = null;
		for (var i=0; i<menu.length; ++i) {
			menuitem = menu[i];
			if (menuitem.cid == cid)
				break;
		}
		//
		for (var j=0; j<menuitem.disheslist.length; ++j) {
			dishes = menuitem.disheslist[j];
			if (dishes.did == did) {
				if (dishes.buycount > 0) {
					dishes.buycount=0;
				}
				break;
			}
		}
	};
}])

//呼叫服务
.service('CallService', ['$resource', function($resource){
	var callres = $resource(baseurl+'/service/:rid/:dtid');//呼叫
	
	this.callservant = function(rid, dtid, info){
		return callres.get({rid:rid, dtid:dtid, info:info});
	};
}])

//订单服务
.service('OrderService', ['$resource', '$localStorage', function($resource, $localStorage){
	var orderres = $resource(baseurl+'/order/:rid/:dtid');//订单
	
	//下单
	this.neworder = function(rid, dtid, order_data, successFunc, errorFunc){
		return orderres.save(
			{rid:rid, dtid:dtid}, 
			order_data, 
			function(data){
				if ($localStorage.hisorders) {
					var myDate = new Date();
					order_data.createTime = myDate.getDateTime();
					order_data.totalPrice = 0;
					for (var j=0; j<order_data.allDishes.length; ++j) {
						var dishes = order_data.allDishes[j];
						order_data.totalPrice += dishes.dprice * dishes.total;
					}
					$localStorage.hisorders.unshift(order_data);
				}
				//
				if (successFunc) successFunc(data);
			}, 
			errorFunc
		);
	};
	//获取历史订单
	this.hisorders = function(rid){
		if (!$localStorage.hisorders) {
			$localStorage.hisorders = [];
		}
		return $localStorage.hisorders;
	};
	//获取历史订单总数
	this.hisorder_count = function(rid){
		if (!$localStorage.hisorders) {
			return 0;
		}
		return $localStorage.hisorders.length;
	};
	//清空历史订单
	this.clearhis = function(rid){
		$localStorage.hisorders = [];
	};
}])

//购物车服务
.service('ShoppingCartService', ['$resource', 'MenuService', function($resource, MenuService){
	var goods_list = [];
	//获取总数
	this.getcount = function(){
		var totol_buycount = 0;
		//
		for (var i=0; i<goods_list.length; ++i) {
			var menuitem = goods_list[i];
			for (var j=0; j<menuitem.disheslist.length; ++j) {
				var dishes = menuitem.disheslist[j];
				totol_buycount = totol_buycount+dishes.buycount;
			}
		}
		//
		return totol_buycount;
	};
	//加入购物车
	this.addgoods = function(cid, did){
		MenuService.addgoods(cid, did);
		//
		var menuitem = null;
		for (var i=0; i<goods_list.length; ++i) {
			if (goods_list[i].cid == cid) {
				menuitem = goods_list[i];
				break;
			}
		}
		//
		if (menuitem == null) {
			var detail = MenuService.getdetail(cid, did);
			menuitem = {cid:cid, cname:detail.cname, disheslist:[{did:did, dname:detail.dname, dprice:detail.dprice, dimage:detail.dimage, buycount:1}]};
			goods_list.push(menuitem);
		} else {
			var dishes = null;
			for (var j=0; j<menuitem.disheslist.length; ++j) {
				if (menuitem.disheslist[j].did == did) {
					dishes = menuitem.disheslist[j];
					break;
				}
			}
			if (dishes == null) {
				var detail = MenuService.getdetail(cid, did);
				menuitem.disheslist.push({did:did, dname:detail.dname, dprice:detail.dprice, dimage:detail.dimage, buycount:1});
			} else {
				++dishes.buycount;
			}
		}
	};
	//从购物车移除
	this.removegoods = function(cid, did){
		MenuService.removegoods(cid, did);
		//
		for (var i=0; i<goods_list.length; ++i) {
			var menuitem = goods_list[i];
			if (menuitem.cid == cid) {
				var indextodelete = -1;
				for (var j=0; j<menuitem.disheslist.length; ++j) {
					var dishes = menuitem.disheslist[j];
					if (dishes.did == did) {
						if (dishes.buycount > 1) {
							--dishes.buycount;
						} else {
							indextodelete = j;
						}
						break;
					}
				}
				//
				if (indextodelete >= 0) {
					//console.log("removegoods() indextodelete="+indextodelete);
					menuitem.disheslist.splice(indextodelete, 1);
				}
				//
				break;
			}
		}
	};
	//清空购物车
	this.cleargoods = function(){
		for (var i=0; i<goods_list.length; i++) {
			var menuitem = goods_list[i];
			for (var j=0; j<menuitem.disheslist.length; j++) {
				var dishes = menuitem.disheslist[j];
				//console.log(dishes);
				MenuService.cleangoods(menuitem.cid, dishes.did);
			}
		}
		goods_list = [];
	};
	//获取购物车内容
	this.getcart = function(){
		return goods_list;
	};
	//获取购物车总价格
	this.gettotalprice = function(){
		var totalprice = 0;
		//
		for (var i=0; i<goods_list.length; ++i) {
			var menuitem = goods_list[i];
			for (var j=0; j<menuitem.disheslist.length; ++j) {
				var dishes = menuitem.disheslist[j];
				totalprice += dishes.dprice * dishes.buycount;
			}
		}
		//
		return totalprice;
	};
	//获取购物车所有菜品列表
	this.getalldishes = function() {
		var dishes_list = [];
		//
		for (var i=0; i<goods_list.length; ++i) {
			var menuitem = goods_list[i];
			for (var j=0; j<menuitem.disheslist.length; ++j) {
				var dishes = menuitem.disheslist[j];
				dishes_list.push({did:dishes.did, dname:dishes.dname, dprice:dishes.dprice, total:dishes.buycount});
			}
		}
		//
		return dishes_list;
	};
}])

;

if (!Date.prototype.getDateTime) {
  (function() {

    function pad(number) {
      if (number < 10) {
        return '0' + number;
      }
      return number;
    }

    Date.prototype.getDateTime = function() {
      return this.getFullYear() +
        '-' + pad(this.getMonth() + 1) +
        '-' + pad(this.getDate()) +
        ' ' + pad(this.getHours()) +
        ':' + pad(this.getMinutes()) +
        ':' + pad(this.getSeconds())
        ;
    };
  }());
}
